package com.arcaneshift.servlet.action.game;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.arcaneshift.manager.action.game.SpellCardManager;
import com.arcaneshift.manager.interfaces.ISpellCardManager;
import com.arcaneshift.model.action.game.SpellCard;
import com.arcaneshift.model.request.AddSpellCardRequest;
import com.arcaneshift.model.response.AddSpellCardResponse;
import com.arcaneshift.model.validators.AddSpellCardValidator;
import com.arcaneshift.model.validators.system.SessionValidator;
import com.arcaneshift.model.validators.user.AdminValidator;
import com.arcaneshift.security.errors.CardError;
import com.arcaneshift.security.errors.UserError;
import com.arcaneshift.security.exceptions.CardException;
import com.arcaneshift.servlet.util.network.RequestAction;
import com.arcaneshift.servlet.util.parser.AddSpellCardXMLParser;
import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.validators.ActionValidator;
import com.common.security.abstracts.AbstractError;
import com.common.security.abstracts.AbstractException;
import com.common.security.abstracts.AbstractRuntimeException;
import com.common.security.errors.ParserError;
import com.common.security.exceptions.ManagerException;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractParserServlet;
import com.common.servlet.interfaces.IParser;

public class AddSpellCardServlet extends AbstractParserServlet {
	private static final long serialVersionUID = 1L;
	private static final Set<AbstractValidator> validators = new HashSet<AbstractValidator>();

	private AddSpellCardRequest requestContent;
	private AddSpellCardXMLParser parser = new AddSpellCardXMLParser();
	private ISpellCardManager spellCardManager = new SpellCardManager();
	
	static {
		validators.add(new ActionValidator(RequestAction.ADD_SPELL_CARD));
		validators.add(new SessionValidator());
		validators.add(new AdminValidator());
		validators.add(new AddSpellCardValidator());
	}
	
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) {
		try {
			AddSpellCardResponse addSpellCardResponse = new AddSpellCardResponse();
			SpellCard card = new SpellCard();
			
			card.setName(requestContent.getName());
			card.setElement(requestContent.getElement());
			spellCardManager.save(card);
			
			addSpellCardResponse.setRequestAction(requestContent.getAction());
			
			response.setContentType("text/xml");
			response.getWriter().write(parser().parseResponse(addSpellCardResponse));
			response.getWriter().close();
		} catch (ManagerException managerException) {
			exception(managerException, response);
		} catch (ParserException parserException) {
			exception(parserException, response);
		} catch (IOException ioException) {
			error(new ParserError(ParserError.STREAM_ERROR), response);
		} catch (CardException cardException) {
			exception(cardException, response);
		}
	}

	@Override
	public void setRequest(AbstractRequest abstractRequest) {
		requestContent = (AddSpellCardRequest) abstractRequest;
	}

	@Override
	public Set<AbstractValidator> getValidators() {
		return validators;
	}

	@Override
	public IParser parser() {
		return parser;
	}
	
	@Override
	public void exception(AbstractException exception, HttpServletResponse response) {
		super.exception(exception, response);
		
		switch (exception.getCode()) {
			 case ManagerException.DATABASE_EXCEPTION: {
				 error(new UserError(AbstractError.DATABASE_ERROR), response);
				 break;
			 }
		}
	}
	
	@Override
	public void exception(AbstractRuntimeException exception, HttpServletResponse response) {
		switch (exception.getCode()) {
			case CardException.ELEMENT_TYPE_NOT_AVAILABLE: {
				error(new CardError(CardError.ELEMENT_NOT_EXIST), response);
				break;
			}
		}
	}
}
